2 * This file contains test for both fptoa and fptoms (which uses dofptoa),
3 * since all these functions are very similar.
7 #include "ntp_stdlib.h"
10 #define SFP_MAX_PRECISION 6
13 void test_PositiveInteger(void);
14 void test_NegativeInteger(void);
15 void test_PositiveIntegerPositiveFraction(void);
16 void test_NegativeIntegerNegativeFraction(void);
17 void test_PositiveIntegerNegativeFraction(void);
18 void test_NegativeIntegerPositiveFraction(void);
19 void test_SingleDecimalInteger(void);
20 void test_SingleDecimalRounding(void);
32 void test_PositiveInteger(void)
34 s_fp test = 300 << 16; // exact 300.000000
36 TEST_ASSERT_EQUAL_STRING("300.000000", fptoa(test, SFP_MAX_PRECISION));
37 TEST_ASSERT_EQUAL_STRING("300000.000", fptoms(test, SFP_MAX_PRECISION));
40 void test_NegativeInteger(void)
42 s_fp test = -(200 << 16); // exact -200.000000
44 TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION));
45 TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION));
48 void test_PositiveIntegerPositiveFraction(void)
50 s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5
52 TEST_ASSERT_EQUAL_STRING("300.500000", fptoa(test, SFP_MAX_PRECISION));
53 TEST_ASSERT_EQUAL_STRING("300500.000", fptoms(test, SFP_MAX_PRECISION));
56 void test_NegativeIntegerNegativeFraction(void)
58 s_fp test = -(200 << 16) - (1 << 15); // -200 - 0.5
60 TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION));
61 TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION));
64 void test_PositiveIntegerNegativeFraction(void)
66 s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25
68 TEST_ASSERT_EQUAL_STRING("299.750000", fptoa(test, SFP_MAX_PRECISION));
69 TEST_ASSERT_EQUAL_STRING("299750.000", fptoms(test, SFP_MAX_PRECISION));
72 void test_NegativeIntegerPositiveFraction(void)
74 s_fp test = -(200 << 16) + (1 << 14)*3; // -200 + 0.75
76 TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION));
77 TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION));
80 void test_SingleDecimalInteger(void)
82 s_fp test = 300 << 16; // 300
84 TEST_ASSERT_EQUAL_STRING("300.0", fptoa(test, 1));
85 TEST_ASSERT_EQUAL_STRING("300000.0", fptoms(test, 1));
88 void test_SingleDecimalRounding(void)
90 s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75
92 TEST_ASSERT_EQUAL_STRING("2.8", fptoa(test, 1));
93 TEST_ASSERT_EQUAL_STRING("2750.0", fptoms(test, 1));